package com.example.adms.message.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.adms.businessTrip.entity.BusinessTrip;
import com.example.adms.leave.entity.Leave;
import com.example.adms.message.entity.Message;
import com.example.adms.message.mapper.MessageMapper;
import com.example.adms.message.service.MessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hyt
 * @since 2023-04-24
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {
    @Autowired
    MessageMapper messageMapper;

    @Override
    public Map<String, Integer> hasEvent(String userId) {
        Integer role = messageMapper.selectUserRoleIdByUserId(userId);
        Map<String, Integer> map = new HashMap<>();
        if (role == 1){
            //管理员
            Integer leaveListNum = messageMapper.selectUnPassLeave();
            Integer businessTripListNum = messageMapper.selectUnPassBusinessTrip();
            map.put("leaveListNum",leaveListNum);
            map.put("businessTripListNum",businessTripListNum);
            map.put("msg",0);
            return map;
        }
        else if (role == 2){
            //部长
            Integer departId = messageMapper.selectDepartIdByUserId(userId);
            Integer leaveListNum = messageMapper.leaderSelectUnPassLeave(departId);
            Integer businessTripListNum = messageMapper.leaderSelectUnPassBusinessTrip(departId);
            Integer msg = messageMapper.selectMessageByUserId(userId);
            map.put("leaveListNum",leaveListNum);
            map.put("businessTripListNum",businessTripListNum);
            map.put("msg",msg);
            return map;
        }
        else if (role == 3){
            Integer msg = messageMapper.selectMessageByUserId(userId);
            map.put("leaveListNum",null);
            map.put("businessTripListNum",null);
            map.put("msg",msg);
            return map;
        }
        else {
            map.put("leaveListNum",null);
            map.put("businessTripListNum",null);
            map.put("msg",null);
            return map;
        }
    }

    @Override
    public boolean clearUnreadMsg(String userId) {
        UpdateWrapper<Message> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("`read`",0).eq("`receiver`",userId).set("`read`",1);
        messageMapper.update(null,updateWrapper);
        return true;
    }
}
